package top.yehot.kettle.boot.core;

import java.util.Properties;

import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.plugins.PluginFolder;
import org.pentaho.di.core.plugins.StepPluginType;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import top.yehot.kettle.boot.core.config.KettleCfg;
import top.yehot.kettle.boot.core.utils.FileUtil;

/**
 * Kettle 初始化
 * 
 * @author lquan
 * @date 2022年5月29日 下午3:56:39
 */
@Component
@Order(-1)
@DependsOn("kettleCfg")
public class KettleInit implements InitializingBean {

	@Override
	public void afterPropertiesSet() throws Exception {
		environmentInit();
		KettleEnvironment.init();
	}
	
	private void environmentInit() {
		Properties properties = System.getProperties();
		
		properties.put("KETTLE_HOME", KettleCfg.home);
		if (StringUtils.hasText(KettleCfg.pluginPackages)) {
			properties.put("KETTLE_PLUGIN_PACKAGES", FileUtil.replaceSeparator(KettleCfg.pluginPackages));
			properties.put("KETTLE_PLUGIN_BASE_FOLDERS", FileUtil.replaceSeparator(KettleCfg.pluginPackages));
			// 加载Kettle插件
			StepPluginType.getInstance().getPluginFolders().add(new PluginFolder(KettleCfg.pluginPackages, false, true));
		}
    }

}
